package cn.hbwe.service.impl;

import cn.hbwe.Dto.pLoginDTO;
import cn.hbwe.Dto.pRegisterDTO;
import cn.hbwe.mapper.DoctorMapper;
import cn.hbwe.pojo.entity.Doctor;
import cn.hbwe.pojo.entity.Patient;
import cn.hbwe.mapper.PatientMapper;
import cn.hbwe.service.IPatientService;
import cn.hbwe.util.CustomException;
import cn.hbwe.util.constant.MessageConstant;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 7组
 * @since 2023-10-18
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements IPatientService {
    @Autowired
    private PatientMapper patientMapper;
    /**
     * 用户登录
     * @param ploginDTO
     * @return
     */
    @Override
    public Patient plogin(pLoginDTO ploginDTO) {
        //条件构造器
        LambdaQueryWrapper<Patient> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Patient::getPatientAccount, ploginDTO.getPatientAccount());
        lqw.orderByDesc(Patient::getPatientId);

        //查询
        Patient patient = this.getOne(lqw);

        //是否查询到此用户
        if (patient == null) {
            throw new CustomException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //md5密码加密
        String password = ploginDTO.getPatientPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //密码是否正确
        if (!patient.getPatientPassword().equals(password)) {
            throw new CustomException(MessageConstant.PASSWORD_ERROR);
        }

        //返回结果
        return patient;
    }

    /**
     * 用户注册
     * @param pregisterDTO
     * @return
     */
    @Override
    public Patient pregister(pRegisterDTO pregisterDTO) {
        //条件构造器
        LambdaQueryWrapper<Patient> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Patient::getPatientAccount, pregisterDTO.getPatientAccount());

        //查询,判断新的用户名是否存在
        long count = count(lqw);
        if (count > 0) {
            throw new CustomException(MessageConstant.ALREADY_EXISTS);
        }




        //md5加密
        String password = pregisterDTO.getPatientPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        Patient patient = Patient.builder()
                .patientName(pregisterDTO.getPatientName())
                .patientPassword(password)
                .patientPhone(pregisterDTO.getPatientPhone())
                .patientAccount(pregisterDTO.getPatientAccount())
                .patientEmail(pregisterDTO.getPatientEmail())
                .patientAge(pregisterDTO.getPatientAge())
                .patientSex(pregisterDTO.getPatientSex())
                .build();

        //添加
        this.save(patient);

        //返回
        return patient;
    }


    @Override
    public List<Patient> getPatientId(int id) {
        LambdaQueryWrapper<Patient> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Patient::getPatientId,id);
        List<Patient> bannerItems = patientMapper.selectList(lqw);
        return  bannerItems;
    }

    @Override
    public Patient updatePatient(pRegisterDTO pregisterDTO) {
        //条件构造器
        LambdaQueryWrapper<Patient> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Patient::getPatientAccount, pregisterDTO.getPatientAccount());
        //md5加密
        String password = pregisterDTO.getPatientPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        Patient patient = Patient.builder()
                .patientId(pregisterDTO.getPatientId())
                .patientName(pregisterDTO.getPatientName())
                .patientPassword(password)
                .patientPhone(pregisterDTO.getPatientPhone())
                .patientAccount(pregisterDTO.getPatientAccount())
                .patientEmail(pregisterDTO.getPatientEmail())
                .patientAge(pregisterDTO.getPatientAge())
                .patientSex(pregisterDTO.getPatientSex())
                .build();

        //添加
        this.updateById(patient);

        //返回
        return patient;
    }

    @Override
    public IPage<Patient> getPage(Long currentPage, Long pageSize, String name) {
        Page<Patient> page = Page.of(currentPage, pageSize);
        LambdaQueryWrapper<Patient> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Patient::getPatientName, name);
        Page<Patient> patientPage = page(page, lqw);

        return patientPage;
    }



    //----------查询用户名字--------------

    public String pname(int id){
        Patient patient = patientMapper.selectById(id);
        String pname= patient.getPatientName();

        return pname;
    }



}
